package com.wenan.ui.controls.listBoxes
{
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.DataGrid;
	import mx.controls.Image;
	
	public class SortRenderer extends Image
	{
		[Embed(source="images/asc.png")]
		private var ascIcon:Class;
		[Embed(source="images/desc.png")]
		private var descIcon:Class;
		[Embed(source="images/noSort.png")]
		private var noSort:Class;
		
		
		public function SortRenderer()
		{
			super();
			this.verticalCenter="0";
			this.horizontalCenter="0";
			this.width = 10;
			this.height = 11;
			this.scaleContent = false;
			this.addEventListener(MouseEvent.CLICK, changeSortHandler);
		}
		
		override public function set data(value:Object):void
		{
			super.data = value;
			if (value.sort == "asc")
			{
				this.source = ascIcon;
			} else if (value.sort == "desc")
			{
				this.source = descIcon;
			} else 
			{
				this.source = noSort;
			}
		}
		
		protected function changeSortHandler(event:MouseEvent):void
		{
			changeAllState();
			var image:Image = event.target as Image;
			if (image.source == noSort)
			{
				this.source = ascIcon;
				data.sort = "asc";
			} else if (image.source == ascIcon)
			{
				this.source = descIcon;
				data.sort = "desc";
			}else if (image.source == descIcon)
			{
				this.source = noSort;
				data.sort = "";
			}
		}
		
		public function changeAllState():void
		{
			var dg:DataGrid = DataGrid(listData.owner);
			var s:ArrayCollection = ArrayCollection(dg.dataProvider);  
			for(var i:int=0;i<s.length;i++)
			{ 
				if(s[i]["sort"] != "")
				{ 
					s[i]["sort"] = "";
				}                 
			}
			dg.invalidateList();
		}
	}
}